# syntax=docker/dockerfile:1

# This file is part of linuxserver.io project
# https://github.com/linuxserver/docker-kasm-cursor
# https://github.com/linuxserver/docker-kasm-cursor/pkgs/container/kasm-cursor
#
# SPDX-License-Identifier: GPL-3.0-only
# Copyright (c) 2024 LinuxServer.io

FROM lscr.io/linuxserver/kasm:latest

# set version label
ARG BUILD_DATE
ARG VERSION
ARG CURSOR_VERSION
LABEL build_version="Linuxserver.io version:- ${VERSION} Build-date:- ${BUILD_DATE}"
LABEL maintainer="linuxserver.io"

# environment variables
ENV DEBIAN_FRONTEND=noninteractive

# 中文语言和时间格式环境变量
ENV LANG=zh_CN.UTF-8
ENV LANGUAGE=zh_CN:zh
ENV LC_ALL=zh_CN.UTF-8
ENV LC_CTYPE=zh_CN.UTF-8
ENV LC_NUMERIC=zh_CN.UTF-8
ENV LC_TIME=zh_CN.UTF-8
ENV LC_COLLATE=zh_CN.UTF-8
ENV LC_MONETARY=zh_CN.UTF-8
ENV LC_MESSAGES=zh_CN.UTF-8
ENV LC_PAPER=zh_CN.UTF-8
ENV LC_NAME=zh_CN.UTF-8
ENV LC_ADDRESS=zh_CN.UTF-8
ENV LC_TELEPHONE=zh_CN.UTF-8
ENV LC_MEASUREMENT=zh_CN.UTF-8
ENV LC_IDENTIFICATION=zh_CN.UTF-8

# 时区设置（默认中国时区）
ENV TZ=Asia/Shanghai

# Cursor编辑器中文环境
ENV CURSOR_LANG=zh-CN
ENV CURSOR_LOCALE=zh_CN.UTF-8

# install cursor dependencies and Chinese language support
RUN \
    apt-get update && \
    apt-get install -y \
        fuse \
        libfuse2 \
        libgtk-3-0 \
        libx11-xcb1 \
        libxss1 \
        libgconf-2-4 \
        libxrandr2 \
        libasound2 \
        libpangocairo-1.0-0 \
        libatk1.0-0 \
        libcairo-gobject2 \
        libgtk-3-0 \
        libgdk-pixbuf2.0-0 \
        libxcomposite1 \
        libxcursor1 \
        libxdamage1 \
        libxext6 \
        libxfixes3 \
        libxi6 \
        libxrandr2 \
        libxrender1 \
        libxss1 \
        libxtst6 \
        ca-certificates \
        fonts-liberation \
        libappindicator1 \
        libnss3 \
        lsb-release \
        xdg-utils \
        wget \
        curl \
        # 中文字体和语言包支持
        locales \
        language-pack-zh-hans \
        language-pack-zh-hans-base \
        fonts-noto-cjk \
        fonts-wqy-microhei \
        fonts-wqy-zenhei \
        fonts-droid-fallback \
        ttf-wqy-microhei \
        ttf-wqy-zenhei && \
    # 生成中文语言环境
    locale-gen zh_CN.UTF-8 && \
    update-locale LANG=zh_CN.UTF-8 && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# download and install cursor
RUN \
    CURSOR_VERSION=${CURSOR_VERSION:-latest} && \
    if [ "$CURSOR_VERSION" = "latest" ]; then \
        CURSOR_URL="https://downloader.cursor.sh/linux/appImage/x64"; \
    else \
        CURSOR_URL="https://downloader.cursor.sh/linux/appImage/x64?version=${CURSOR_VERSION}"; \
    fi && \
    curl -L "${CURSOR_URL}" -o /tmp/cursor.AppImage && \
    chmod +x /tmp/cursor.AppImage && \
    mkdir -p /app/cursor && \
    /tmp/cursor.AppImage --appimage-extract-and-run --help > /dev/null 2>&1 || true && \
    /tmp/cursor.AppImage --appimage-extract > /dev/null 2>&1 && \
    if [ -d "squashfs-root" ]; then \
        cp -r squashfs-root/* /app/cursor/ && \
        rm -rf squashfs-root; \
    fi && \
    rm -f /tmp/cursor.AppImage

# create cursor launcher script
RUN \
    cat > /usr/local/bin/cursor << 'EOF'
#!/bin/bash
export DISPLAY=${DISPLAY:-:0}
export PULSE_RUNTIME_PATH=/var/run/pulse
export PULSE_COOKIE=/tmp/pulse-cookie

# 设置中文环境变量
export LANG=zh_CN.UTF-8
export LANGUAGE=zh_CN:zh
export LC_ALL=zh_CN.UTF-8
export LC_CTYPE=zh_CN.UTF-8
export LC_TIME=zh_CN.UTF-8
export LC_MESSAGES=zh_CN.UTF-8

# 设置Cursor中文环境
export CURSOR_LANG=zh-CN
export CURSOR_LOCALE=zh_CN.UTF-8

# Start cursor with proper environment
exec /app/cursor/cursor "$@"
EOF
    chmod +x /usr/local/bin/cursor

# create desktop entry
RUN \
    mkdir -p /usr/share/applications && \
    cat > /usr/share/applications/cursor.desktop << 'EOF'
[Desktop Entry]
Version=1.0
Type=Application
Name=Cursor
Comment=The AI-first code editor
Exec=cursor %F
Icon=cursor
Terminal=false
Categories=Development;TextEditor;
MimeType=text/plain;text/x-chdr;text/x-csrc;text/x-c++hdr;text/x-c++src;text/x-java;text/x-dsrc;text/x-pascal;text/x-perl;text/x-python;application/x-php;application/x-httpd-php3;application/x-httpd-php4;application/x-httpd-php5;application/javascript;application/json;text/xml;text/html;text/css;text/x-sql;text/x-diff;
EOF

# copy local files
COPY root/ /

# ports and volumes
EXPOSE 3000 3001

VOLUME /config
